#ifdef GL_ES
precision highp float;
#endif

uniform vec3 uLensS;
uniform vec2 uLensF;
uniform vec2 uFov;

uniform sampler2D uSampler;

varying vec3 vPosition;
varying vec2 vTextureCoord;

vec2 GLCoord2TextureCoord(vec2 glCoord) {
	return glCoord  * vec2(1.0, -1.0)/ 2.0 + vec2(0.5, 0.5);
}

void main(void){
	float scale = uLensS.z;
	vec3 vPos = vPosition;
	float Fx = uLensF.x;
	float Fy = uLensF.y;

	vec2 vMapping = vPos.xy;
	vMapping.x = vMapping.x + ((pow(vPos.y, 2.0)/scale)*vPos.x/scale)*-Fx;
	vMapping.y = vMapping.y + ((pow(vPos.x, 2.0)/scale)*vPos.y/scale)*-Fy;
	vMapping = vMapping * uLensS.xy;

	vMapping = GLCoord2TextureCoord(vMapping/scale);

    vMapping.x = vMapping.x + uFov.x;
    vMapping.y = vMapping.y + uFov.y;

	vec4 texture = texture2D(uSampler, vMapping);
	if(vMapping.x > 0.99 || vMapping.x < 0.01 || vMapping.y > 0.99 || vMapping.y < 0.01){
		texture = vec4(0.0, 0.0, 0.0, 1.0);
	}
	gl_FragColor = texture;
}
